<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ConfigController extends Controller
{
	//显示网站配置
	public function index(){
		//查询数据
		$config=DB::table('config')->first();
		//分配数据
		return view('config.adminConfig',['config'=>$config]);
	}
   
	//添加网站配置
	public function addConf(){
		return view('config.addConf');
	}
  
	//添加网站配置
	public function insertConf(Request $request){
		//接收表单传递过来的值
		$config=$request->only(['title','keywords','description','logo','status']);
		//上传图片
		if($request->hasFile('logo')){
			//文件路径
			$dir='uploads/';
			//获取后缀名
			$result=$request->file('logo')->getclientOriginalExtension();
			//上传文件名
			$fname=uniqid().'.'.$result;
			//dd($fname);
			//设置文件路径
			$config['logo']=$dir.$fname;
			//移动图片
			$request->file('logo')->move($dir,$fname);
		}else{
			//上传失败
			return redirect('/config/addConf');
		}
		//插入数据库
		$result1=DB::table('config')->insert($config);
		
		if($result1){
			return redirect('/config/adminConfig');
		}else{
			return redirect('/config/addConf');
		}
	}
   
    //修改网站配置模板
	public function updateConf($id){
		//查询数据根据id
		$config=DB::table('config')->where('id','=',$id)->first();
		//分配数据
		return view('config.updateConf',['config'=>$config]);
	}
   
	//修改网站配置操作
	public function updateConfig(Request $request){
		//接收表单传递过来的值
		$config=$request->only('title','keywords','description','logo','status');
		///dd($config);
		//上传图片
		if($request->hasFile('logo')){
			//文件路径
			$dir='uploads/';
			//获取后缀名
			$result=$request->file('logo')->getclientOriginalExtension();
			//上传文件名
			$fname=uniqid().'.'.$result;
			//dd($fname);
			//设置文件路径
			$config['logo']=$dir.$fname;
			//移动图片
			$request->file('logo')->move($dir,$fname);
		}else{
			//不上传
			$result1=DB::table('config')->first();
			//dd($result1['logo']);
			$config['logo']=$result1->logo;
		}
		//修改数据
		$result=DB::table('config')->where('id','=',$request->only('id'))->update($config);
		if($result){
			return redirect('/config/adminConfig/');
		}else{
			return redirect("/config/updateConf/{$request->only('id')}");
		}
	}
   
   
}
